//
//  SDHSportsMineHeaderView.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/6.
//

#import "SDHSportsMineHeaderView.h"
#import "SDHSportsMineHeaderButton.h"

const static CGFloat kButtonGap = 50;
const static NSInteger kBtnPersonalInfoTag = 1000;
const static CGFloat kBtnWidth = 66;
const static CGFloat kBtnHeight = 96;

@interface SDHSportsMineHeaderView ()

@property (nonatomic, strong) UIImageView *adsImgView;
@property (nonatomic, strong) UIView *personalServiceView;

@end

@implementation SDHSportsMineHeaderView

- (id)init {
    if (self = [super init]) {
//        self.backgroundColor = [UIColor systemPinkColor];
        UIView *loginView = [UIView new];
//        loginView.backgroundColor = [UIColor blueColor];
        [self addSubview:loginView];
        self.loginView = loginView;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loginClick:)];
        [self.loginView addGestureRecognizer:tap];
        
        UIImageView *avatarImgView = [UIImageView new];
        avatarImgView.image = [UIImage imageNamed:@"touxiang"];
        [loginView addSubview:avatarImgView];
        self.avatarImgView = avatarImgView;
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adsClick:)];
        
        UILabel *lbSign = [UILabel new];
        self.lbSign = lbSign;
        lbSign.text = @"Sign in/login";
        lbSign.textColor = [UIColor blackColor];
//        lbSign.backgroundColor = [UIColor redColor];
        lbSign.textAlignment = NSTextAlignmentLeft;
        lbSign.font = [UIFont boldSystemFontOfSize:15];
        lbSign.numberOfLines = 1;
        [loginView addSubview:lbSign];
        
        
        UILabel *lbSignDes = [UILabel new];
        lbSignDes.text = @"Sign in to enjoy more services";
        lbSignDes.font = [UIFont systemFontOfSize:12];
        lbSignDes.textColor = [UIColor grayColor];
//        lbSignDes.backgroundColor = [UIColor yellowColor];
        lbSignDes.textAlignment = NSTextAlignmentLeft;
        [loginView addSubview:lbSignDes];
        self.lbSignDes = lbSignDes;
        
        UIImageView *adsView = [UIImageView new];
        adsView.image = [UIImage imageNamed:@"banner2"];
        [self addSubview:adsView];
        self.adsImgView = adsView;
        
        UILabel *LbPersonalInfo = [UILabel new];
        LbPersonalInfo.text = @"Personal service";
        LbPersonalInfo.font = [UIFont boldSystemFontOfSize:16];
        LbPersonalInfo.textColor = [UIColor blackColor];
//        LbPersonalInfo.backgroundColor = [UIColor greenColor];
        [self addSubview:LbPersonalInfo];
        
        SDHSportsMineHeaderButton *btnFollow = [SDHSportsMineHeaderButton new];
        btnFollow.btnTitle.text = @"Follow";
        btnFollow.btnImg.image = [UIImage imageNamed:@"guanzhu"];
        btnFollow.tag = kBtnPersonalInfoTag + 1;
        [self addSubview:btnFollow];
        
        SDHSportsMineHeaderButton *btnBrowse = [SDHSportsMineHeaderButton new];
        btnBrowse.btnTitle.text = @"Browse";
        btnBrowse.btnImg.image = [UIImage imageNamed:@"guanzhu-1"];
        btnBrowse.tag = kBtnPersonalInfoTag + 2;
        [self addSubview:btnBrowse];
        
        SDHSportsMineHeaderButton *btnInfo = [SDHSportsMineHeaderButton new];
        btnInfo.btnTitle.text = @"Infomation";
        btnInfo.btnImg.image = [UIImage imageNamed:@"xiaoxi"];
        btnInfo.tag = kBtnPersonalInfoTag + 3;
        [self addSubview:btnInfo];
        
        __weak typeof(self) weakSelf = self;
        btnFollow.block = ^(NSInteger index) {
            [weakSelf personalInfoClick:index];
        };
        btnBrowse.block = ^(NSInteger index) {
            [weakSelf personalInfoClick:index];
        };
        btnInfo.block = ^(NSInteger index) {
            [weakSelf personalInfoClick:index];
        };
        
        [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf).offset(10);
            make.left.equalTo(weakSelf).offset(20);
            make.right.equalTo(weakSelf).offset(-20);
            make.height.equalTo(@50);
        }];
        
        [avatarImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.loginView).offset(0);
            make.left.equalTo(weakSelf.loginView).offset(0);
            make.width.equalTo(@50);
            make.height.equalTo(@50);
        }];
        
        [lbSign mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(avatarImgView.mas_right).offset(10);
            make.top.equalTo(avatarImgView).offset(10);
        }];
        
        [lbSignDes mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lbSign);
            make.top.equalTo(lbSign.mas_bottom).offset(5);
        }];
        
        [self.adsImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.loginView.mas_bottom).offset(10);
            make.left.equalTo(weakSelf.loginView);
            make.width.equalTo(weakSelf.loginView);
            make.height.equalTo(@66);
        }];
        
        [LbPersonalInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.adsImgView.mas_bottom).offset(15);
            make.left.equalTo(weakSelf.adsImgView);
        }];
        
        [btnFollow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(LbPersonalInfo.mas_bottom).offset(15);
            make.right.equalTo(btnBrowse.mas_left).offset(-kButtonGap);
            make.width.equalTo(@(kBtnWidth));
            make.height.equalTo(@(kBtnHeight));
        }];
        
        [btnBrowse mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btnFollow.mas_top);
            make.centerX.equalTo(weakSelf);
            make.width.equalTo(@(kBtnWidth));
            make.height.equalTo(@(kBtnHeight));
        }];
        
        [btnInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btnFollow);
            make.left.equalTo(btnBrowse.mas_right).offset(kButtonGap);
            make.width.equalTo(@(kBtnWidth));
            make.height.equalTo(@(kBtnHeight));
        }];
    }
    
    return self;
}

- (void)loginClick:(UITapGestureRecognizer *)gesture {
    if (self.loginClick) {
        self.loginClick();
    }
}

- (void)adsClick:(UITapGestureRecognizer *)gesture {
    if (self.adsClick) {
        self.adsClick();
    }
}

- (void)personalInfoClick:(NSInteger)index {
    if (self.personInfoClick) {
        self.personInfoClick(index);
    }
}

- (void)login {
    self.lbSignDes.hidden = YES;
    [self.lbSign mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.loginView);
    }];
}

- (void)logout {
    _lbSignDes.hidden = NO;
    _lbSign.text = @"Sign in/login";
    [_lbSign mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_avatarImgView.mas_right).offset(10);
        make.top.equalTo(_avatarImgView).offset(10);
    }];
    
    [_lbSignDes mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lbSign);
        make.top.equalTo(_lbSign.mas_bottom).offset(0);
    }];
}

@end
